import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('../views/Dashboard.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/shop-info',
    name: 'ShopInfo',
    component: () => import('../views/ShopInfo.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/foods',
    name: 'Foods',
    component: () => import('../views/Foods.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/orders',
    name: 'Orders',
    component: () => import('../views/Orders.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/statistics',
    name: 'Statistics',
    component: () => import('../views/Statistics.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/wallet',
    name: 'BusinessWallet',
    component: () => import('../views/BusinessWallet.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/comments',
    name: 'BusinessComments',
    component: () => import('../views/BusinessComments.vue'),
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('businessToken')
  const businessId = localStorage.getItem('businessId')
  
  if (to.meta.requiresAuth) {
    if (token && businessId) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
